package com.kehao.account_book.controller;

import com.google.gson.Gson;
import com.kehao.account_book.pojo.User;
import com.kehao.account_book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;

/**
 * @author kehao
 * @date 2020/2/14-15:37
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public String login(@RequestBody String json){
        User user = new Gson().fromJson(json,User.class);
        if (userService.checkPassword(user)){
            System.out.println("login:ok");
            return "ok";
        }else{
            System.out.println("login:false");
            return "fail";
        }
    }

    @RequestMapping("register")
    public String register(@RequestBody String json){
        User user = new Gson().fromJson(json,User.class);
        if(userService.addUser(user)){
            return "ok";
        }else {
            return "fail";
        }
    }

    @RequestMapping("changepassword")
    public String changePassword(@RequestBody String json){
        User user = new Gson().fromJson(json,User.class);
        if(userService.changePassword(user)){
            return "ok";
        }else {
            return "fail";
        }
    }

    @RequestMapping("test")
    public String test(){
        return "test";
    }
}
